<template>
	<view class="container">
		<!-- 退款/退货申请 -->
		<!-- 订单商品 -->
		<view class="goods" v-for="(item, index) in orderData.order_goods">
			<image class="goodsImg" :src="item.goods_image" mode=""></image>
			<view class="goodsMsg">
				<view class="goods_name">{{ item.goods_name }}</view>
				<view class="goods_spec"><text v-if="item.sku_id">{{ item.sku_name }}</text><text>数量: {{ item.buy_num }}</text></view>
			</view>
		</view>
		<!-- 申请原因 -->
		<view class="because">
			<view class="title" @click="openPop">
				<text style="color: rgba(155, 155, 155, 1);">申请原因</text>
				<text v-if="!requestName">请选择申请原因</text>
				<text v-if="requestName">{{ requestName }}</text>
			</view>
			<view class="write">
				<textarea placeholder-class="l60" value="" v-model="refund_remark" placeholder="说出你申请售后的原因，文字在500字以内，图片最多3张" />
			</view>
			<view class="pic">
				<view class="image">
					<u-upload
						:action="action"
						:custom-btn="true"
						max-count='3'
						:header="header"
						name='file'
						:multiple="true"
						:deletable='false'
						:show-progress="false"
						@on-success="successUpload"
					>
						<view slot="addBtn" class="slot-btn" hover-class="slot-btn__hover" hover-stay-time="150">
							<u-icon name="photo" size="60" :color="$u.color['lightColor']"></u-icon>
						</view>
					</u-upload>
				</view>
			</view>
		</view>
		<view class="backAddress" v-if="num == 1">
			<view class="ad">收货地址</view>
			<view class="msg">
				<view class="left">
					<view><text>{{ orderData.store.refund_name }}</text><text style="margin-left: 20rpx;">{{ orderData.store.refund_phone }}</text></view>
					<view>{{ orderData.store.refund_address }} </view>
				</view>
				<view class="right">
					
				</view>
			</view>
		</view>
		<view class="btn" @click="submit">
			提交
		</view>
		<!-- 申请原因弹出层 -->
		<view class="pop" @click="closePop" v-if="showPop">
			<view class="popView" @click.stop='showPop = true'>
				<view class="title">
					申请原因
					<view><image src="https://community.chuangxiangdianli.com/images/order/close.png" mode=""></image></view>
				</view>
				<view class="items">
					<view class="item" v-for="(item, index) in becauseList" @click.stop='choseItem(item.id, item.name)'>
						<view>{{ item.name }}</view>
						<view class="pic_img">
							<image v-if="item.id == requestId" src="https://community.chuangxiangdianli.com/images/order/radio_s.png" mode=""></image>
							<image v-if="item.id != requestId" src="https://community.chuangxiangdianli.com/images/order/radio_n.png" mode=""></image>
						</view>
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import baseUrl from '../../utils/commonrequest.js'
	export default{
		data() {
			return {
				action: '',
				header: {},
				baseUrl: baseUrl.baseUrl,
				showPop: false,
				num: 0,  // 0: 退款， 1：退货
				orderId: '',
				orderData: {}, // 售后数据
				becauseList: [],  // 选择售后原因
				requestId: 0, // 申请售后的原因
				refund_remark: '', // 退款说明
				images: [],  // 申请售后原因土图片
				requestName: '', // 申请售后原因
				
			}
		},
		onLoad(option) {
			this.action = this.baseUrl + 'Mini/refund/refund_upload'
			this.header.Authorization = 'Bearer ' + uni.getStorageSync('userInfo').token
			if( option.orderId ){
				this.num = option.num
				this.orderId = option.orderId
				this.refundBefore()
			}
			this.returnCuase()
		},
		
		methods:{
			// 申请售后原因
			returnCuase() {
				var that = this
				that.$mrequest({
					url: 'Mini/refund/getRefundCate',
					method: 'POST',
					callBack: function callBack (res) {
						console.log('请求成功!', res)
						that.becauseList = res.data
					},
					fail: function fail (res) {
						console.log('请求失败!', res)
					}
				})
			},
			// 请求数据
			refundBefore() {
				let that = this
				that.$mrequest({
					url: 'Mini/refund/refund_before',
					method: 'POST',
					data: {
						order_id: that.orderId
					},
					callBack: function callBack (res) {
						console.log('请求成功！', res);
						if (res.status) {
							that.orderData = res.data
						}
					},
					fail: function fail (res) {
						uni.showToast({
							title: res.msg,
							icon: 'none'
						})
						console.log('请求失败！', res);
					}
				})
			},
			openPop() {
				this.showPop = true
			},
			choseItem(id, name) {
				this.requestId = id
				this.requestName = name
				this.showPop = false
			},
			//  上传图片返回
			successUpload(data, index, lists, name) {
				if (data.status) {
					this.images.push(data.data)
					console.log('打印image', this.images)
				}
			},
			// 点击提交申请
			submit() {
				var that = this
				if (!that.requestId) {
					uni.showToast({
						title: "请选择申请选项！",
						icon: 'none'
					})
					return
				}
				if (JSON.stringify(that.images) == '[]') {
					uni.showToast({
						title: "请上传说明图片！",
						icon: 'none'
					})
					return
				}
				if (!that.refund_remark) {
					uni.showToast({
						title: "请输入申请原因！",
						icon: 'none'
					})
					return
				}
				that.$mrequest({
					url: 'Mini/refund/addRefund',
					method: 'POST',
					data: {
						order_id: that.orderId,
						refund_type: that.num,
						refund_remark: that.refund_remark,
						images: JSON.stringify(that.images),
						refund_cate_id: that.requestId
					},
					callBack: function callBack (res) {
						console.log('申请提交成功', res)
						if (res.status) {
							uni.showToast({
								title: res.msg,
								icon: 'none'
							})
							uni.navigateTo({
								url: './returnList'
							})
						}
						
					},
					fail: function fail (res) {
						console.log('申请提交失败', res)
						uni.showToast({
							title: res.msg,
							icon: 'none'
						})
					}
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.container{
		background-color: rgba(239, 240, 247, 1);
	}
	.goods{
		display: flex;
		flex-direction: row;
		justify-content: flex-start;
		background-color: #fff;
		width: 100%;
		border-radius: 20rpx;
		padding: 40rpx 30rpx;
		box-sizing: border-box;
		.goodsImg{
			display: block;
			height: 110rpx;
			width: 110rpx;
		}
		.goodsMsg{
			display: flex;
			flex-direction: column;
			justify-content: space-between;
			margin-left: 51rpx;
			font-size: 28rpx;
			font-family: PingFang SC;
			font-weight: 500;
			.goods_spec{
				font-size: 24rpx;
				font-family: PingFang SC;
				font-weight: 500;
				&>text:nth-child(1){
					margin-right: 30rpx;
				}
			}
		}
	}
	.because{
		width: 100%;
		background-color: #fff;
		border-radius: 20rpx;
		padding: 0 30rpx 30rpx;
		box-sizing: border-box;
		margin-top: 15rpx;
		.title{
			display: flex;
			flex-direction: row;
			justify-content: space-between;
			align-items: center;
			height: 120rpx;
			border-bottom: 2rpx solid rgba(191, 191, 191, 1);
			
			font-size: 24rpx;
			font-family: PingFang SC;
			font-weight: 500;
			color: #333333;
		}
		.write{
			height: 194rpx;
			width: 100%;
			background: #EFF0F7;
			border-radius: 10rpx;
			margin: 40rpx 0;
			font-size: 28rpx;
			font-family: PingFang SC;
			font-weight: 400;
			color: #999999;
			&>textarea{
				width: 100%;
				height: 100%;
			}
			.l60{
				line-height: 60rpx;
			}
		}
		.pic{
			width: 100%;
			height: 200rpx;
			// padding-left: 30rpx;
			box-sizing: border-box;
			.u-list-item.data-v-0d33af60{
				margin: 0!important;
			}
		}
	}
	.backAddress{
		width: 100%;
		background-color: #fff;
		border-radius: 20rpx;
		margin-top: 15rpx;
		font-size: 30rpx;
		font-family: PingFang SC;
		font-weight: 500;
		color: #333333;
		.ad{
			width: 100%;
			padding: 0 30rpx;
			height: 80rpx;
			line-height: 80rpx;
			border-bottom: 2rpx solid rgba(236, 238, 238, 1);
		}
		.msg{
			display: flex;
			flex-direction: row;
			justify-content: space-between;
			width: 100%;
			padding: 25rpx 30rpx 25rpx;
			box-sizing: border-box;
			.left{
				display: flex;
				flex-direction: column;
				justify-content: space-between;
				width: 100%;
				&>view:nth-child(1){
					margin-bottom: 30rpx;
					// box-sizing: border-box;
				}
			}
			.right{
				display: flex;
				justify-content: center;
				align-items: center;
				
			}
		}
	}
	.btn{
		height: 90rpx;
		margin: 150rpx 30rpx 0rpx;
		background: linear-gradient(-74deg, #70DAC4, #3EC4A7);
		border-radius: 45rpx;
		line-height: 90rpx;
		font-size: 36rpx;
		font-family: PingFang SC;
		font-weight: 500;
		color: #FFFFFF;
		text-align: center;
	}
	.pop{
		position: fixed;
		left: 0;
		top: 0;
		height: 100vh;
		width: 100vw;
		z-index: 101;
		background: RGBA(0,0,0, 0.6);
		.popView{
			position: absolute;
			left: 0;
			bottom: 0;
			height: 708rpx;
			width: 100%;
			background-color: #fff;
			border-radius: 30rpx 30rpx 0px 0px;
			.title{
				position: relative;
				height: 161rpx;
				line-height: 160rpx;
				text-align: center;
				font-size: 36rpx;
				font-family: PingFang SC;
				font-weight: bold;
				color: #333333;
				&>view{
					position: absolute;
					right: 30rpx;
					top: 0;
					bottom: 0;
					margin: auto;
					width: 28rpx;
					height: 28rpx;
					&>image{
						display: block;
						width: 100%;
						height: 100%;
					}
				}
			}
			.items{
				width: 100%;
				padding: 0 30rpx;
				box-sizing: border-box;
				.item{
					display: flex;
					flex-direction: row;
					justify-content: space-between;
					align-items: center;
					height: 91rpx;
					.pic_img{
						height: 30rpx;
						width: 30rpx;
						&>image{
							display: block;
							width: 100%;
							height: 100%;
						}
					}
					
				}
			}
		}
	}
</style>
